﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using WindowsSignInPage;

namespace LN.WebFileManager.Upload
{
    public partial class ProgressBar : System.Web.UI.Page
    {
        LoginUser loginUser;
        protected string SessionID
        {
            get
            {
                return Request.Form["SessionID"];
            }
        }

        protected string UploadId
        {
            get
            {
                string uploadId = Request["UploadID"];
                if (string.IsNullOrEmpty(uploadId))
                    return SessionID;
                else
                    return uploadId;
            }
        }
        public string Domain
        {
            get
            {
                return "ceibs.edu";
            }
        }

        public string UserName
        {
            get
            {
                return loginUser.LoginId;
            }
        }

        public string Password
        {
            get
            {
                return EncryptTool.Decrypt(loginUser.Pwd);
            }
        }

        protected string CookieUserName
        {
            get 
            {
                HttpCookie cookie = Request.Cookies[UploadSession];
                if (cookie != null)
                    return cookie.Value;
                else
                {
                    return Request.QueryString["username"];
                }
            }
        }
        protected bool ImpersonateUser()
        {
            string currentUser = Request.RequestType == "POST"
                ? CookieUserName
                : Authentication.GetCurrentUser();

            
            if (string.IsNullOrEmpty(currentUser))
            {
                string logonUrl = Authentication.GetLogonUrl();
                Response.Redirect(logonUrl);
            }

            loginUser = new LoginUser(currentUser);
            return WindowsContext.Instance.ImpersonateValidUser(UserName, Domain, Password);
        }
        const string UploadSession = "sp_upload_session";
        
        protected override void OnPreLoad(EventArgs e)
        {
            string currentUser = Authentication.GetCurrentUser();
            if (!string.IsNullOrEmpty(currentUser))
            {
                HttpCookie cookie = new HttpCookie(UploadSession, currentUser);
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.SetCookie(cookie);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UploadId))
                return;

            UploadContext upload = UploadContextFactory.GetUploadContext(UploadId);

            if (upload == null)
                return;

            if (Request.Files.Count > 0 && ImpersonateUser())
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFile file = Request.Files[i];
                    if (null == file || file.ContentLength == 0)
                    {
                        return;
                    }

                    string filename = Path.GetFileName(file.FileName);
                    string fullFilename = Path.Combine(upload.TmepFileDir, filename);

                    file.SaveAs(fullFilename);
                }
                
            }
        }


        protected void btn_cancle_Click(object sender, EventArgs e)
        {

            if (!ClientScript.IsClientScriptBlockRegistered("closeScript"))
                ClientScript.RegisterClientScriptBlock(this.GetType(), "closeScript", "<script>window.opener=self;window.close();</script>");
        }
    }
}
